package com.mxy.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapEncoder;
import com.mxy.myapplication.R;
import com.mxy.myapplication.activity.MainActivity;
import com.mxy.myapplication.activity.MyWebView_Activity;
import com.mxy.myapplication.util.Util;

import java.util.ArrayList;
import java.util.Random;

/**
 * 瀑布流RecyclerView适配器
 */

public class AdapterForRv_Waterfall extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context cont;
    private LayoutInflater lf;
    private Random random=new Random();
    private ArrayList<Integer> photolist=new ArrayList<Integer>();
    private ArrayList<Integer> itemHights=new ArrayList<Integer>();//保存item的高度，防止瀑布的item在滑动过程中随机移动
    private StaggeredGridLayoutManager staggeredGridLayoutManager;

    public AdapterForRv_Waterfall(Context cont,StaggeredGridLayoutManager staggeredGridLayoutManager){
        this.cont=cont;
        this.lf=LayoutInflater.from(cont);
        this.staggeredGridLayoutManager=staggeredGridLayoutManager;
        photolist.add(R.drawable.jj1);
        photolist.add(R.drawable.jj2);
        photolist.add(R.drawable.jj3);
        photolist.add(R.drawable.bleach1);
        photolist.add(R.drawable.bleach2);
        photolist.add(R.drawable.bleach3);
        photolist.add(R.drawable.guanlan1);
        photolist.add(R.drawable.guanlan2);
        photolist.add(R.drawable.guanlan3);

        //注：如果是动态的列表，那么这几行代码应该写成一个单独的方法，在每次集合的数量发生变化后、执行notifyDataSetChanged方法刷新列表之前调用
        //并且已经存在的高度数据应该被保留，只增加新进item的高度即可
        for(int i=itemHights.size();i<photolist.size();i++){//预先加载与item数量相符的高度数量
            itemHights.add(Util.dip2px((MainActivity)cont,random.nextInt(300)+100));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder=new MyViewHolder(this.lf.inflate(R.layout.item_recyclerview_waterfall,parent,false));
//        if(viewType==0){//根据viewType设置header或footer
//            StaggeredGridLayoutManager.LayoutParams lp=(StaggeredGridLayoutManager.LayoutParams) myViewHolder.itemView.getLayoutParams();
//            lp.setFullSpan(true);//StaggeredGridLayoutManager.LayoutParams特有的方法，合并整行单元格
//        }
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder=(MyViewHolder) holder;
        ViewGroup.LayoutParams layoutParams=myViewHolder.iv.getLayoutParams();

        layoutParams.height=itemHights.get(position);//取出已保存高度，这样item在高度固定的前提下，就不会在发生挪动效果
        myViewHolder.iv.setOnClickListener(new onclickListener(position));

        Glide.with(cont)
                .load(photolist.get(position))
                .asBitmap()
                .encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG,100))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontAnimate()
                .animate(R.anim.alpha)
                .into(myViewHolder.iv);
    }

    @Override
    public int getItemCount() {
        return photolist.size();
    }

    /**
     * 自定义ViewHolder
     */
    class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView iv;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv=(ImageView) itemView.findViewById(R.id.imageview);
        }
    }

    /**
     * 为item增加点击事件
     */
    class onclickListener implements View.OnClickListener{

        int position;

        public onclickListener(int position){
            this.position=position;
        }

        @Override
        public void onClick(View v) {
            cont.startActivity(new Intent(cont, MyWebView_Activity.class));
        }
    }
}